//
//  FaBiaoViewController.m
//  ShangChengMoBan
//
//  Created by yaoqi yaoqi on 16/9/2.
//  Copyright © 2016年 ping. All rights reserved.
// 发表评论界面

#import "FaBiaoViewController.h"
#import "SCHomeViewController.h"
@interface FaBiaoViewController ()<UITextViewDelegate>

@end

@implementation FaBiaoViewController
{ UIView * _headerView;
    NSMutableArray * _fenArray;
    NSMutableArray * _styleArray;
    NSMutableArray * _butArray;
    UILabel * _styleLab,*_placeLable;
    UITextView *family;
    int _Xing ;//选择的星数量
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self headerView];
    [self content];
    [self footView];
}
#pragma mark 上部星级界面设计
-(void)headerView{
    [self drawNavWithTitle:@"发表评论" WithColor:[UIColor colorWithHue:0.00 saturation:0.00 brightness:1.00 alpha:1.00]];
    [self drawBackButton];

    _fenArray = [NSMutableArray array];
    _styleArray = [NSMutableArray array];
    _butArray = [NSMutableArray array];
    NSArray * arr = @[@"差",@"一般",@"满意",@"很满意",@"强烈推荐"];
     _headerView = [[UIView alloc]initWithFrame:CGRectMake(0, 64, KScreenWidth, 2)];
    _headerView.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.86 alpha:1.00];
    [self.view addSubview:_headerView];
    UILabel *title = [[UILabel alloc]initWithFrame:CGRectMake(0, _headerView.bottom+15, KScreenWidth, 20)];
    title.text = @"亲,动动手指给我们打个分吧~";
    title.textAlignment = NSTextAlignmentCenter;
    title.font = [UIFont systemFontOfSize:14];
    title.textColor = [UIColor grayColor];
    [self.view addSubview:title];
    CGFloat btn_jianGe = 20*_kscW;
    CGFloat btn_height = 30*_kscH;
    CGFloat btn_weidth = btn_height;
    CGFloat btn_x = (KScreenWidth-5*btn_height-4*btn_jianGe)/2;
      for (int i =0; i<5; i++) {
          UIButton * XingBtn = [[UIButton alloc]initWithFrame:CGRectMake(btn_x+(btn_weidth+btn_jianGe)*i, title.bottom+12, btn_weidth, btn_height)];
          XingBtn.tag = i;
          [XingBtn addTarget:self action:@selector(take:) forControlEvents:UIControlEventTouchUpInside];
          [XingBtn setBackgroundImage:[UIImage imageNamed:@"五角星"] forState:UIControlStateNormal];
          [self.view addSubview:XingBtn];
          [_butArray addObject:XingBtn];
          UILabel * fenLab = [[UILabel alloc]initWithFrame:CGRectMake(XingBtn.left, XingBtn.bottom+8, btn_weidth, 15)];
          fenLab.text = [NSString stringWithFormat:@"%i分",i+1];
          fenLab.font = [UIFont systemFontOfSize:12];
          fenLab.tag = i;
          fenLab.textAlignment = NSTextAlignmentCenter;
          fenLab.textColor = [UIColor lightGrayColor];
          [self.view addSubview:fenLab];
          [_fenArray addObject:fenLab];
           _styleLab = [[UILabel alloc]initWithFrame:CGRectMake(XingBtn.left-15, fenLab.bottom+2, btn_weidth+30, 15)];
         _styleLab.text = arr[i];
          _styleLab.tag = i;
          _styleLab.textAlignment = NSTextAlignmentCenter;
          _styleLab.font = [UIFont systemFontOfSize:12];
          _styleLab.textColor = [UIColor grayColor];
          [self.view addSubview:_styleLab];
          [_styleArray addObject:_styleLab];
     }
}
#pragma mark 输入发布内容
-(void)content{
       UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, _styleLab.bottom+15, KScreenWidth, 1)];
    view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.86 alpha:1.00];
    [self.view addSubview:view];
    UIImageView * image = [[UIImageView alloc]initWithFrame:CGRectMake(12, view.bottom+10, 50*_kscW, 50*_kscW)];
    image.image = [UIImage imageNamed:@"ii"];
    image.contentMode =  UIViewContentModeScaleAspectFit;
    [self.view addSubview:image];
    family = [[UITextView alloc]initWithFrame:CGRectMake(image.right+5, view.bottom+10, KScreenWidth-image.right-5, 150*_kscH)];
    family.delegate = self;
    [self.view addSubview:family];
    _placeLable = [[UILabel alloc]initWithFrame:CGRectMake(10, 0, 200, 30)];
    _placeLable.text = @"亲,您的好评是我们前进的动力~";
    _placeLable.textColor = [UIColor lightGrayColor];
    _placeLable.font = [UIFont systemFontOfSize:13];
    _placeLable.backgroundColor = [UIColor clearColor];
    [family addSubview:_placeLable];
 }
#pragma mark 点击输入框调用
-(void)textViewDidChange:(UITextView *)textView{
    if (textView.text.length == 0) {
        _placeLable.text = @"亲,您的好评是我们前进的动力~";
    }else{
        _placeLable.text = @"";
    }
}
#pragma mark 点击选择星级
-(void)take:(UIButton *)button{
     int tag = (int)button.tag;
    _Xing = tag+1;
    for (int i =0; i<5; i++) {
        if (((UIButton *)_butArray[i]).tag<=tag) {
            [((UIButton *)_butArray[i]) setBackgroundImage:[UIImage imageNamed:@"评价里的星"] forState:UIControlStateNormal] ;
        }else{
        [((UIButton *)_butArray[i]) setBackgroundImage:[UIImage imageNamed:@"五角星"] forState:UIControlStateNormal] ;
        }
        if (((UILabel *)_fenArray[i]).tag == tag) {
            ((UILabel *)_fenArray[tag]).textColor = [UIColor colorWithHue:0.10 saturation:0.92 brightness:0.94 alpha:1.00];
        }else{
        ((UILabel *)_fenArray[i]).textColor = [UIColor lightGrayColor];;
        }
        if (((UILabel *)_styleArray[i]).tag == tag) {
            ((UILabel *)_styleArray[i]).textColor =[UIColor colorWithHue:0.10 saturation:0.92 brightness:0.94 alpha:1.00];
        }else{
         ((UILabel *)_styleArray[i]).textColor =[UIColor grayColor];;
        }
    }
}
#pragma mark 发布按钮
-(void)footView{
    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, family.bottom+15, KScreenWidth, 1)];
    view.backgroundColor = [UIColor colorWithHue:0.00 saturation:0.00 brightness:0.86 alpha:1.00];
    [self.view addSubview:view];
    UIButton * button  = [[UIButton alloc]initWithFrame:CGRectMake(10, (KScreenHeight-view.bottom)/2-20+view.bottom, KScreenWidth-20, 40)];
    [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [button setTitle:@"发布评论" forState:UIControlStateNormal];
    button.backgroundColor =[UIColor colorWithHue:0.10 saturation:1.00 brightness:0.99 alpha:1.00];
    button.layer.cornerRadius = 5;
    [button addTarget:self action:@selector(faBu) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:button];
}
#pragma mark 点击发布
-(void)faBu{
    [family resignFirstResponder];
    if (_Xing == 0) {
        [[iToast makeText:@" 请给商品打分! "] show];
    }else if (family.text.length == 0 ){
    [[iToast makeText:@" 请输入评论内容! "] show];
    }else{
        NSMutableDictionary * dic = [[NSMutableDictionary alloc]init];
        [dic setObject:[NSString stringWithFormat:@"%d",_Xing] forKey:@"commentRank"];
        [dic setObject:family.text forKey:@"content"];
        [dic setObject:_orderID forKey:@"orderId"];
        [dic setObject:[self defaultLoadUserName] forKey:@"userName"];
        //   发布网络请求
        [NetWorking NEWjieKouQingQiu:DD_commentOrder ANDIP:IP And:dic anBlock:^(NSDictionary *dic, NSError *error) {
            if (!error) {
                if ([dic[Code] integerValue]== 200) {
                    [[iToast makeText:@"  发布成功!  "] show];
                    if (_timp == 1) {
                    [self.navigationController popViewControllerAnimated:YES];
                    }else{
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    }
                    [[NSNotificationCenter defaultCenter]postNotificationName:@"检测状态" object:nil];
                }
            }
        }];

    }
    }

@end
